package main

import (
	"Agricultural/application/blockchain"
	"Agricultural/application/controller"
	"github.com/gin-gonic/gin"
	"net/http"
)

func setupRouter() *gin.Engine  {
	router := gin.Default()

	// 中间件
	router.Use(controller.CORS)
	router.Use(controller.FixVueRouter)

	// 加载中间件
	router.Use(controller.CORS)         // 先加载跨域模块（便于开发）
	router.Use(controller.FixVueRouter) // 修复前端内部路由问题

	// 公司
	router.GET("/queryCompany", controller.QueryCompany)
	router.GET("/queryAllCompany", controller.QueryAllCompany)
	router.GET("/querySubAccount", controller.QuerySubAccount)
	router.GET("/querySubAccounts", controller.QuerySubAccounts)

	router.POST("/createCompany", controller.CreateCompany)
	router.POST("/updateCompany", controller.UpdateCompany)
	router.POST("/createSubAccount", controller.CreateSubAccount)
	router.POST("/updateSubAccount", controller.UpdateSubAccount)
	router.POST("/delCompany", controller.DelCompany)
	router.POST("/delSubAccount", controller.DelSubAccount)

	// 场地
	router.GET("/querySpace", controller.QuerySpace)

	router.POST("/createSpace", controller.CreateSpace)
	router.POST("/updateSpace", controller.UpdateSpace)
	router.POST("/delSpace", controller.DelSpace)

	// 资产
	router.GET("/queryAssets", controller.QueryAssets)

	router.POST("/createAssets", controller.CreateAssets)
	router.POST("/updateAssets", controller.UpdateAssets)
	router.POST("/delAssets", controller.DelAssets)

	// 肥力
	router.GET("/queryPick", controller.QueryPick)

	router.POST("/createTestFertility", controller.CreateTestFertility)
	router.POST("/createAddFertility", controller.CreateAddFertility)
	router.POST("/createCrops", controller.CreateCrops)
	router.POST("/updateCrops", controller.UpdateCrops)
	router.POST("/pickCrops", controller.PickCrops)

	// 订单
	router.GET("/queryOrder", controller.QueryOrder)
	router.GET("/traceBackTo", controller.TraceBackTo)

	router.POST("/createMachining", controller.CreateMachining)
	router.POST("/inStock", controller.InStock)
	router.POST("/outStock", controller.OutStock)
	router.POST("/createOrder", controller.CreateOrder)
	router.POST("/addLogistics", controller.AddLogistics)
	router.POST("/confirmOrder", controller.ConfirmOrder)

	// 用户
	router.POST("/login", controller.Login)

	// 二维码
	router.POST("/createQrCode", controller.CreateQrCode)
	router.GET("/showQrCode", controller.ShowQrCode)

	// 图片
	router.POST("/uploadImg", controller.UploadImg)
	router.GET("/sendCompanyImg", controller.SendCompanyImg)

	// 静态文件路由
	router.StaticFS("/web/", http.Dir("./public/"))

	return router
}

func main()  {
	blockchain.Init()
	router := setupRouter()
	err := router.Run()
	if err != nil {
		panic(err)
	}
}
